package com.xiaoq.matrix.endpoint;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.matrix.activity.vo.ContentViewVo;
import com.xiaoq.matrix.entity.ContentEntity;
import com.xiaoq.matrix.service.IContentService;
import com.xiaoq.matrix.vo.Content4TermVo;
import com.xiaoq.vo.PagingVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
/**
 * 用于首页推荐的头条和活动(推荐感觉后期可以使用大数据的推荐系统)
 * @author wang zhihao
 * 
 *
 */
@RestController
@RequestMapping("/api/v1/content")
public class ContentEndpoint2C {
	
	@Autowired
	private IContentService contentSrv;
	
	@Value("${matrix.serviceUrl}")
	private String serviceUrl;
	
	private static final Logger LOG = LoggerFactory.getLogger(ContentEndpoint2C.class);
	
	private static final class Content4TermListResponse extends GenericListResponse<Content4TermVo> {};
	
	private class ContentViewVoListResponse extends GenericListResponse<ContentViewVo>{}
	
	private class ContentDataResponse extends GenericDataResponse<ContentViewVo>{}
	
	private class ContentShareNum extends GenericDataResponse<Integer>{}
	
	@ApiOperation(value = "内容分享量统计", notes = "内容分享量统计")
	@ApiResponses({ @ApiResponse(code = 200, response = ContentShareNum.class, message = "内容分享量统计") })
	@PutMapping(value = "/shareNum", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> shareNum(@RequestParam(value = "contenId", required = true) String contenId){
		ContentEntity content = contentSrv.findContent(contenId);
		Integer shareNum = content.getShareNum();
		if(shareNum==null){
			shareNum=0;
		}
		shareNum = shareNum+1;
		content.setShareNum(shareNum);
		contentSrv.addContent(content);
		return ContentShareNum.okWithData(shareNum);
		
	}
	
	@ApiOperation(value = "获取内容（旅行头条）列表（首页）", notes = "获取内容（旅行头条）列表（首页）")
	@ApiResponses({ @ApiResponse(code = 200, response = Content4TermListResponse.class, message = "内容（旅行头条）列表") })
	@GetMapping(value = "/{userId}/4homepage", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> contentList(
			@PathVariable(name="userId", required=false) @ApiParam(value="用户Id", required=false) String userId,
			@RequestParam(name="city", required=false) @ApiParam(value="城市code", required=false) String city,
			@RequestParam(name="district", required=false) @ApiParam(value="区县编码", required=false) String district,
			@RequestParam(name="lng", required=false) @ApiParam(value="经度", required=false) Double lng,
			@RequestParam(name="lat", required=false) @ApiParam(value="纬度", required=false) Double lat,
			@ModelAttribute(value="pvo")PagingVo pvo) {
		// 根据城市编码查询内容列表
		// 初期简化实现为：
		// 查询对应城市下的营销内容数据，按照发布时间倒序排列（最近发布的实现在最上面）
		List<ContentEntity> contentList = contentSrv.findContentListByCity(city,district, pvo);
		Number tc = pvo.getTc();
		Long total = 0L;
		if (tc != null) {
			total = tc.longValue();
		}
		List<Content4TermVo> content4TermVoList =  new ArrayList<>();
		Content4TermVo content4TermVo = null;
		if(CollectionUtils.isNotEmpty(contentList)){
			for(ContentEntity contentEntity: contentList){
				String id = contentEntity.getId();
				String staticPath = serviceUrl+"/api/v1/socials/content?contentId="+id;
				content4TermVo = new Content4TermVo();
				BeanUtils.copyProperties(contentEntity, content4TermVo);
				content4TermVo.setStaticPath(staticPath);
				content4TermVoList.add(content4TermVo);
				
			}
			
		}else{
			LOG.warn("查询结果为空！");
		}
		return Content4TermListResponse.listAndCount(content4TermVoList, total);
	}
	
	@ApiOperation(value = "获取内容（旅行头条）列表（发现页）", notes = "获取内容（旅行头条）列表（发现页）")
	@ApiResponses({ @ApiResponse(code = 200, response = ContentViewVoListResponse.class, message = "内容（旅行头条）列表") })
	@GetMapping(value = "/4findpage", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> contentList(
			@RequestParam(name="city", required=false) @ApiParam(value="城市行政编码", required=true) String city,
			@ModelAttribute(value="pvo")PagingVo pvo) {
		List<ContentEntity> contentList = contentSrv.findContentListByCity(city,null, pvo);
		Number tc = pvo.getTc();
		Long total = 0L;
		if (tc != null) {
			total = tc.longValue();
		}
		List<ContentViewVo> contentViewVoList =  new ArrayList<>();
		ContentViewVo contentViewVo = null;
		if(CollectionUtils.isNotEmpty(contentList)){
			for(ContentEntity contentEntity: contentList){
				String id = contentEntity.getId();
				String staticPath = serviceUrl+"/api/v1/socials/content?contentId="+id;
				contentViewVo = new ContentViewVo();
				BeanUtils.copyProperties(contentEntity, contentViewVo);
				contentViewVo.setStaticPath(staticPath);
				contentViewVoList.add(contentViewVo);
				
			}
			
		}else{
			LOG.warn("查询结果为空！");
		}
		return ContentViewVoListResponse.listAndCount(contentViewVoList, total);
	}
	
	@ApiOperation(value = "获取内容（旅行头条）详情", notes = "获取内容详情")
	@ApiResponses({ @ApiResponse(code = 200, response = ContentDataResponse.class, message = "根据头条Id获取头条详情") })
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getContentDetail(@RequestParam(name="contenId", required=true) @ApiParam(value="文章Id", required=true) String contenId) {
		ContentEntity content = contentSrv.findContent(contenId);
		if(content!=null){
			String id = content.getId();
			String staticPath = serviceUrl+"/api/v1/socials/content?contentId="+id;
			ContentViewVo contentViewVo = new ContentViewVo();
			BeanUtils.copyProperties(content, contentViewVo);
			contentViewVo.setStaticPath(staticPath);
			Integer shareNum = contentViewVo.getShareNum();
			if(shareNum==null){
				contentViewVo.setShareNum(0);
			}
			return ContentDataResponse.okWithData(contentViewVo);
		}
		return GenericResponse.ng("没有查到该条内容！");
		
	}
		

	


}
